#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QTreeWidgetItem* item1 = new QTreeWidgetItem();
    item1->setText(0, "猫");
    ui->treeWidget->addTopLevelItem(item1);

    QTreeWidgetItem* item2 = new QTreeWidgetItem();
    item2->setText(0, "狗");
    ui->treeWidget->addTopLevelItem(item2);

    QTreeWidgetItem* item3 = new QTreeWidgetItem();
    item3->setText(0, "鸟");
    ui->treeWidget->addTopLevelItem(item3);

    QTreeWidgetItem* item4 = new QTreeWidgetItem();
    item4->setText(0, "布偶猫");
    item1->addChild(item4);

    QTreeWidgetItem* item5 = new QTreeWidgetItem();
    item5->setText(0, "缅因猫");
    item1->addChild(item5);

    QTreeWidgetItem* item6 = new QTreeWidgetItem();
    item6->setText(0, "中华田园猫");
    item1->addChild(item6);

    QTreeWidgetItem* item7 = new QTreeWidgetItem();
    item7->setText(0, "哈士奇");
    item2->addChild(item7);

    QTreeWidgetItem* item8 = new QTreeWidgetItem();
    item8->setText(0, "阿拉斯加");
    item2->addChild(item8);

    QTreeWidgetItem* item9 = new QTreeWidgetItem();
    item9->setText(0, "柯基");
    item2->addChild(item9);

    QTreeWidgetItem* item10 = new QTreeWidgetItem();
    item10->setText(0, "鹦鹉");
    item3->addChild(item10);

    QTreeWidgetItem* item11 = new QTreeWidgetItem();
    item11->setText(0, "喜鹊");
    item3->addChild(item11);

    QTreeWidgetItem* item12 = new QTreeWidgetItem();
    item12->setText(0, "蜂鸟");
    item3->addChild(item12);
}


Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_insertTop_clicked()
{
    const QString& cur = ui->lineEdit->text();
    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, cur);
    ui->treeWidget->addTopLevelItem(item);
    ui->lineEdit->setText("");
}

void Widget::on_pushButton_insert_clicked()
{
    QTreeWidgetItem* cur = ui->treeWidget->currentItem();
    const QString& str = ui->lineEdit->text();
    QTreeWidgetItem* ins = new QTreeWidgetItem();
    ins->setText(0, str);
    cur->addChild(ins);
    ui->lineEdit->setText("");
}

void Widget::on_pushButton_deletecurrent_clicked()
{
    QTreeWidgetItem* cur = ui->treeWidget->currentItem();
    QTreeWidgetItem* parent = cur->parent();
    if(!parent)
    {
        int id = ui->treeWidget->indexOfTopLevelItem(cur);
        ui->treeWidget->takeTopLevelItem(id);
    }
    else
        parent->removeChild(cur);
}
